<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_Model_PhotoMapper {
	protected $_db_table;

	public function __construct() {
		$this->_db_table = new Api_Model_DbTable_Photo();
	}

	public function getPhotoById($photo_id, $user_id) {
		$select = $this->_db_table->select()->from('photos', array('*'))->where('photo_id = ?', $photo_id);

		$result = $this->_db_table->fetchAll($select);

		$this->db_like = new Api_Model_DbTable_Like();
		$this->db_comment = new Api_Model_DbTable_Comment();
		$this->db_user = new Api_Model_DbTable_User();

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			//like
			$this->db_tour_like = new Api_Model_DbTable_Like();
			$s_like = $this->db_tour_like->select()->from('likes', array('*'))->where('photo_id = ?', $photo_id);
			$re_like = $this->db_tour_like->fetchAll($s_like);
			$count_like = count($re_like);

			//check like
			$s_clike = $this->db_tour_like->select()->from('likes', array('*'))->where('photo_id = ?', $photo_id)
			->where('user_id = ?', $user_id);
			$re_clike = $this->db_tour_like->fetchAll($s_clike);
			if(count($re_clike) == 0) {
				$liked = 0;
			} else
			$liked = 1;

			//comment
			$s_comment = $this->db_comment->select()->from('comments', array('*'))->where('photo_id = ?', $photo_id);
			$re_comment = $this->db_comment->fetchAll($s_comment);
			if(count($re_comment) == 0) {
				$comment = "";
			} else {
				foreach ($re_comment as $row){
					$select_user = $this->db_user->select()->from('users', array('*'))->where('user_id = ?', $row->user_id);
					$result_user = $this->_db_table->fetchAll($select_user);
					$row_user = $result_user->current();
					//get user info
					$user = array('user_id' => $row_user->user_id,
                                  'username' => $row_user->username,
                                  'fullname' => $row_user->fullname,
                                  'profile_picture' => $row_user->profile_picture
					);

					$comment[] = array('comment' => array('comment_id' => $row->comment_id,
                                                        'content' => $row->content,
                                                        'created_time' => $row->created_time,
                                                        'user' => $user));
				}
			}

			$json = array('like_count' => $count_like,
                            'liked' => $liked,
                            'comments' => $comment);

			return $json;

		}

	}


	public function getAllLocationPhoto($location_id, $photo_limit) {
		$select = $this->_db_table->select()->from('photos', array('*'))->where('location_id = ?', $location_id)
		->limit($photo_limit+30, $photo_limit);

		$result = $this->_db_table->fetchAll($select);

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				$db_user = new Api_Model_DbTable_User();
				$select_user = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->user_id);
				$result_user = $this->_db_table->fetchAll($select_user);
				$row_user = $result_user->current();
				//get user info
				$user = array('user_id' => $row_user->user_id,
                              'username' => $row_user->username,
                              'fullname' => $row_user->fullname,
                              'profile_picture' => $row_user->profile_picture
				);



				$json[] = array('photo' => array('photo_id' => $row->photo_id,
                                                    'photo_link' => $row->photo_link,
                                                    'caption' => $row->caption,
                                                    'created_time' => $row->created_time,
                                                    'user' => $user));

			}
			return $json;
		}
	}

	public function getAllUserPhoto($user_id, $photo_limit) {
		$select = $this->_db_table->select()->from('photos', array('*'))->where('user_id = ?', $user_id)
		->limit($photo_limit+30, $photo_limit);

		$result = $this->_db_table->fetchAll($select);

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				$db_user = new Api_Model_DbTable_User();
				$select_user = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->user_id);
				$result_user = $this->_db_table->fetchAll($select_user);
				$row_user = $result_user->current();
				//get user info
				$user = array('user_id' => $row_user->user_id,
                              'username' => $row_user->username,
                              'fullname' => $row_user->fullname,
                              'profile_picture' => $row_user->profile_picture
				);



				$json[] = array('photo' => array('photo_id' => $row->photo_id,
                                                    'photo_link' => $row->photo_link,
                                                    'caption' => $row->caption,
                                                    'created_time' => $row->created_time,
                                                    'user' => $user));

			}
			return $json;
		}
	}

	public function getAllTourPhoto($tour_id, $photo_limit) {
		$select = $this->_db_table->select()->from('photos', array('*'))->where('photo_id = ?', $tour_id)
		->limit($photo_limit+30, $photo_limit);

		$result = $this->_db_table->fetchAll($select);

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				$db_user = new Api_Model_DbTable_User();
				$select_user = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->user_id);
				$result_user = $this->_db_table->fetchAll($select_user);
				$row_user = $result_user->current();
				//get user info
				$user = array('user_id' => $row_user->user_id,
                              'username' => $row_user->username,
                              'fullname' => $row_user->fullname,
                              'profile_picture' => $row_user->profile_picture
				);



				$json[] = array('photo' => array('photo_id' => $row->photo_id,
                                                    'photo_link' => $row->photo_link,
                                                    'caption' => $row->caption,
                                                    'created_time' => $row->created_time,
                                                    'user' => $user));

			}
			return $json;
		}
	}

	public function photoUpload($data){

		$this->db_user = new Api_Model_DbTable_User();
		$select = $this->db_user->select()->from('users', array('*'))->where('user_id = ?', $data['user_id']);
		$result = $this->db_user->fetchAll($select);
		$row = $result->current();
		$username = $row->username;

		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$currentDateTime = date('Y-m-d H-i-s');
		$currentDate = date('Y-m-d');
		$currentTime = date('H-i-s');
		//xử lý avatar
		$structure = APPLICATION_PATH."/../public/images/".$username."/".$currentDate."/";
		$avatar_base64 = base64_decode($data['photo_encode']);
		$avatar_img = imagecreatefromstring($avatar_base64);
		if (!mkdir($structure, 0777, true)){
			mkdir($structure, 0777, true);
		}

		if ($avatar_img != FALSE){
			$micro = (int)(microtime(true)*1000);
			imagejpeg($avatar_img, $structure.$currentTime.$micro.'.jpg');
			$photo_link = 'images/'.$username.'/'.$currentDate.'/'.$currentTime.$micro.'.jpg';
		}
		$photo = array(
                'tour_id' => $data['tour_id'],
                'location_id' => $data['location_id'],
                'user_id' => $data['user_id'],
                'photo_link' => $photo_link,
                'caption' => $data['caption']);

		$affectedRow = $this->_db_table->insert($photo);
		$lastInsertId = $this->_db_table->getAdapter()->lastInsertId();
		if ($affectedRow){
			$this->_db_table->update(array('created_time' => $currentDateTime),
			array('user_id = ?' => $lastInsertId));
		}

		$return = array('photo_link' => $photo_link);

		return $return;
	}
	 
	public function deletePhoto($photo_id){
		$select = $this->_db_table->select()->from('photos', array('*'))->where('photo_id = ?', $photo_id);
		$result = $this->_db_table->fetchAll($select);
		$row = $result->current();

		$mask = APPLICATION_PATH."/../public/".$row->photo_link;
		array_map("unlink", glob($mask));

		$this->db_like = new Api_Model_DbTable_Like();
		$this->db_comment = new Api_Model_DbTable_Comment();

		$this->db_like->delete('photo_id = ?', $photo_id);
		$this->db_comment->delete('photo_id = ?', $photo_id);
		 
	}

}
